﻿using System;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;

namespace BSDS.Core.Data.Remote
{
    public abstract class ResponseParserBase
    {
        protected string GetLink(XDocument document, string xmlns, string name)
        {
            return document.Descendants(XName.Get("Link", xmlns))
                .Where(l => l.Attributes("role").Count() > 0 && l.Attribute("role").Value == name)
                .Select(e => e.Value)
                .FirstOrDefault();
        }

        protected DateTime? GetCompletionDate(XDocument document, string xmlns)
        {
            var completedDate = document.Descendants(XName.Get("DataflowJob", xmlns))
                .Select(l => l.Element(XName.Get("CompletedDate", xmlns)))
                .First().Value;

            if (string.IsNullOrEmpty(completedDate))
            {
                return null;
            }

            return DateTime.Parse(completedDate, CultureInfo.InvariantCulture);
        }
    }
}